<template>
  <a-modal
    title="编辑租户"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item style="display: none;" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-row>
          <a-col :span="12">
            <a-form-item label="租户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
              <a-input
                placeholder="请输入租户名称"
                v-decorator="['name', { rules: [{ required: true, message: '请输入租户名称！' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司全称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
              <a-input
                placeholder="请输入公司全称"
                v-decorator="['companyName', { rules: [{ required: true, message: '请输入公司全称！' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
              <a-input
                placeholder="请输入联系人"
                v-decorator="['contactName', { rules: [{ required: true, message: '请输入联系人！' }] }]"
              />
            </a-form-item>

          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
              <a-input
                placeholder="请输入联系电话"
                v-decorator="['contactMobile', { rules: [{ required: true, message: '请输入联系电话！' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="过期时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-date-picker
                placeholder="请选择过期时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                v-decorator="['expireTime', { rules: [{ required: true, message: '请选择过期时间！' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="用户账号">
              <a-input
                placeholder="请输入租户账号"
                v-decorator="['account', { rules: [{ required: true, message: '请输入租户账号！' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { sysTenantEdit } from '@/api/modular/system/roleManage'
import moment from 'moment'
export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    edit(record) {
      this.visible = true
      setTimeout(() => {
        this.form.setFieldsValue({
          id: record.id,
          name: record.name,
          contactName: record.contactName,
          contactMobile: record.contactMobile,
          companyName: record.companyName,
          expireTime: moment(record.expireTime, 'YYYY-MM-DD'),
          account: record.account
        })
      }, 100)
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.expireTime = moment(values.expireTime).format("YYYY-MM-DD HH:mm:ss");
          sysTenantEdit(values)
            .then(res => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('编辑失败：' + res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },

    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
